/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ","ajaxServ","datagridServ","sysmanMain","btnServ","bizProto"], 
	function($,msgServ,ajaxServ,datagridServ,sysmanMain,btnServ,bizProto) {

	'use strict';

	var    $typedg,$typegrid,$edit_window,_defaultConfig,
			$panel,_bizProto,_moduleConfig,_buttonConfig,
			$edit_window_height=700,$edit_window_width=900,
			docdetailEditUrl,
			doctypeQryUrl='/departmentHis/query/getTreeList.action',
			
			deptOwnerUrl = '/departmentHis/query/getTreeListByOwner.action',
			deptJobUrl = '/departmentHis/query/getTreeListByJobDept.action',
			
			deptSelectUrl='/common/query/deptSelect.action',
			window_id='win-dept-select',
			window_height='400',window_width='600',
			
			select_type = {
				owner:'owner',
				job:'job',
				manageDept:'manageDept'
			},
			//脚本打开页面传递参数
			openParam
		   	
    
	var	private_method = {
		init : function(_options) {
			
			_moduleConfig = sysmanMain.getCurrentModuConfig();
			
			if (window.console) {
				console.info(_moduleConfig.fullBizcode +' modules gets ready and been loaded.');
			}
			_defaultConfig ={
				doc:null,
				rootId:null,				
				panelSelector:'div.well',
				subSystemCode:'sysman',
				moduleCode:'docdetail',
				btnSelector:'td.doc-type-list-btn',
				doccode:null
			}
					
			_defaultConfig = $.extend({},_defaultConfig,_options)			
			_bizProto = new bizProto(_defaultConfig);			
			
			
			$edit_window = $('#win-dept-select');
				
			_buttonConfig={
				"选择":private_method.select,
				"重置":private_method.reset,
			    "取消":private_method.cancel	
			}
			
			private_method.initElement(_bizProto.pageDoc)
		},
		
		
		initElement: function (doc) {
			
			
			
			//初始化操作按钮
			var listTd = $(doc).find(_defaultConfig.btnSelector)
			
						
			$typedg = $(doc).find("table.dg-type");
			
			_bizProto.gridHeight -=100
			var typeParam = {
				height : _bizProto.gridHeight,
				url : _bizProto.rootPath + doctypeQryUrl
				
			}
			
			//设置初始化url
			if(openParam && openParam.selectType){
				var _url
				switch(openParam.selectType){
					case select_type.owner:
						_url = _bizProto.rootPath + deptOwnerUrl;
						break;
					case select_type.job:
						_url = _bizProto.rootPath + deptJobUrl;
						break;	
				}
				typeParam.url =_url
				if(openParam.subsystem){
					typeParam.url =typeParam.url+'?subsystem='+openParam.subsystem
				}
				
				
			}
			if(_defaultConfig.doccode){
				typeParam.queryParams={doccode:_defaultConfig.doccode}
			}
			$typegrid=$($typedg).treegrid(typeParam);		
			
			$($typegrid).treegrid('options').url = _bizProto.rootPath + doctypeQryUrl,
			private_method.bindBtnEvent()
			
		},
		
		searchFunction:function(value,name){
			var _param = {}
	    	_param['searchName'] = name;_param['searchValue'] = value
	    	return _param;
		},
		
		
		//绑定按钮事件
		bindBtnEvent:function(){
			// 绑定按钮事件
			var btn = $(_bizProto.pageDoc).find(_defaultConfig.btnSelector+' a.function-btn')
			$(btn).unbind('click');
			$(btn).bind('click', private_method.function_btn_listenner)			
			
			
		},
		// 功能按钮监听函数
		function_btn_listenner : function(e) {

			btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

		},
		// 新增函数
		select : function() {
			var selectedRecord =$($typegrid).treegrid('getSelected')
			if(!selectedRecord){
				msgServ.Msgslide('请选择部门');
				return
			}
			var _tmpId =$($edit_window).attr('callback-id')
			
			
			//未设置dom参数，脚本打开的窗口
			if(!_tmpId ||$.trim(_tmpId)==''){
				private_method.scriptOpen()
			}
			
			var _tmpValue=$($edit_window).attr('callback-value')
			
			$('#'+_tmpId).val(selectedRecord.id)
			var  _tmpValueElement= $('#'+_tmpValue)
			$(_tmpValueElement).textbox('setText',selectedRecord.name)
			$(_tmpValueElement).textbox('setValue',selectedRecord.name)
			$edit_window.window('close')
		},
		
		/**
		 * 脚本参数回调方法
		 */
		scriptOpen:function(){
			var _defaultConfig = {				
				deptid:null,
				deptname:null
			}
			
			//参数判断
			if(openParam){
				
				var records = $($typedg).datagrid('getSelections')
				_defaultConfig = $.extend({},_defaultConfig,openParam)
				
				var ids,names
				for(var i=0;i<records.length;i++){
					ids = !ids?records[i].id:ids+','+records[i].id
					names = !names?records[i].name:names+','+records[i].name
				}
				
				$('#'+_defaultConfig.deptid).val(ids)
				
				$('#'+_defaultConfig.deptname).textbox('setValue',names)
				
				if(openParam.callback){
					openParam.callback()
				}
				openParam = null;
			}
		},
		// 编辑,flag 为form按钮可见性标志
		reset : function() {
			$typegrid.treegrid('load')							
		},
		
		
		// 删除
		cancel : function() {
			$edit_window.window('close')
		}
		
		,
		onClose:function(){
			$('#'+window_id).empty()
		}
	}
	
	var public_method = {
		/**
		 * @param _options.doc	页面根节点
		 * @param _options.docroot	当前业务页面根节点
		 */
		init:function(_options){
			
			private_method.init(_options)
		},
		/**
		 * 打开部门选择框
		 * @param _options.deptid	
		 * @param _options.deptname
		 * @param _options.selectType	部门选择类型，该参数为空默认显示全部部门
		 * 		'owner':显示人员所在部门及子部门
		 *		'job': 显示岗位角色所在部门及子部门
		 *		'manageDept':显示管理部门及子部门
		 */
		openPage:function(_options){
			var rootpath = '/'+sysmanMain.getRootPath()
			var _config ={
				width:window_width,
				height:window_height,
				window:$('#'+window_id),
				href:rootpath+deptSelectUrl,
				flag:false,
				existCheckElement:'#div-dept-user',
				onClose:private_method.onClose
			}
		
			
					
			var _param = $.extend({},_config,_options)
			openParam = _param
			datagridServ.windowEdit(_param);			
		
		}
	}	
	return public_method;
});